$().ready(function() {
	validateRule();
	initChecked();
	initRound();
	checkedHospital();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$("#amenityIds").val(getChecked());
	$.ajax({
		cache : true,
		type : "POST",
		url : "/usabb/amenityRelation/save-ids",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function getChecked() {
	var adIds = "";
	$("input:checkbox[name=amenity]:checked").each(function (i) {
		if (0 == i) {
			adIds = $(this).val();
		} else {
			adIds += ("," + $(this).val());
		}
	});
	return adIds;
}

//选中 checkbox
function initChecked() {
    var checkedIds = $("#checkedAmenityIds").val();  //后台传到前台
    var answerInt = commaStrToArray(checkedIds);

    var adIds = 0;
    $("input:checkbox[name=amenity]").each(function (i) {
        adIds = parseInt($(this).val());
        if (jQuery.inArray(adIds, answerInt) >= 0) {
            $(this).prop('checked', true);
        }
    });
    return adIds;
}

//选中 hospital checkbox
function checkedHospital() {
    var checkedIds = $("#checkedHospitalIds").val();  //后台传到前台
    var answerInt = commaStrToArray(checkedIds);

    var curId = 0;
    $("input:checkbox[name=hospitalId]").each(function (i) {
        curId = parseInt($(this).val());
        if (jQuery.inArray(curId, answerInt) >= 0) {
            $(this).prop('checked', true);
        }
    });
    return curId;
}

//周边, 如果是新增,显示 div:addDiv otherwise 显示 div:updateDiv
function initRound() {
    var checkedIds = $("#checkedAmenityIds").val();  //后台传到前台
    var answerInt = commaStrToArray(checkedIds);

    var adIds = 0;
    $("input:checkbox[name=amenity]").each(function (i) {
        adIds = parseInt($(this).val());
        if (jQuery.inArray(adIds, answerInt) >= 0) {
            $(this).prop('checked', true);
        }
    });
    return adIds;
}

/**
 * @param str "[30, 25, 18, 15, 10, 5, 1]"
 * return type is array, [30, 25, 18, 15, 10, 5, 1]
 *
 */
function commaStrToArray(str) {
    var answerInt = [];
    var tempArray = str.replace('[', '').replace(']', '').split(',');
    tempArray.forEach(function (item) {
        answerInt.push(parseInt(item))
    });

    return answerInt;
}


function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}


//新增row
function appendRow(currentTable) {
    var pureRow = "<tr>\n" +
        "  <td>\n" +
        "      <select class=\"form-control m-b\" name=\"roundTypeId\">\n" +
        "      <option value=\"1\">商场</option>\n" +
        "      <option value=\"2\">医院</option>\n" +
        "      <option value=\"3\">餐厅</option>\n" +
        "      <option value=\"4\">景点</option>\n" +
        "      </select>" +
        "  </td>\n" +
        "  <td>\n" +
        "      <input name=\"roundName\" class=\"form-control\" type=\"text\">\n" +
        "  </td>\n" +
        "  <td>\n" +
        "      <input name=\"autoDuration\" class=\"form-control\" type=\"text\">\n" +
        "  </td>\n" +
        "  <td>\n" +
        "      \n" +
        "  </td>\n" +
        "  <td>\n" +
        "      <button type=\"button\" class=\"btn btn-danger\" onclick=\"removeRow(this)\">删除</button> \n" +
        "  </td>\n" +
        "</tr>";

    $('.itemId_' + currentTable).append(pureRow);
}


function removeRow(obj) {
    $(obj).parents("tr").remove();
    console.log('remove row done.');

}

